package com.jianbing.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.jianbing.entity.City;
import com.jianbing.exception.ServiceException;
import com.jianbing.mongo.MongoHelper;

/**
 * Created by Han, Tixiang on 2016/8/30 0030.
 */
public class JSONHelper {

    private static final ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null)
                return "";
            return v;
        }
    };

    /***
     * JSON字符串转化为Netty中的ByteBuf
     * @param object
     * @return
     */
    public static String toJSONString(Object object) {
        if (object == null) return "";
        String result = JSONObject.toJSONString(object, filter);
        return result;
    }

    public static <T> T parseObject(String text, Class<T> clazz) {
        return JSON.parseObject(text, clazz);
    }


    public static void main(String[] args) throws ServiceException {
        City city = new City();
        city.setId(1);
        System.out.println(MongoHelper.bean2Document(city));
    }
}
